// 文件存储子服务

#include <brpc/server.h>
#include <butil/logging.h>

#include "etcd.hpp"
#include "logger.hpp"
#include "utils.hpp"
#include "base.pb.h"
#include "file.pb.h"

namespace chen_im
{
    class FileServeiceImpl : public chen_im::FileService
    {
    public:
        FileServeiceImpl(const std::string &storage_path)
            : _storage_path(storage_path)
        {
            umask(0);
            mkdir(storage_path.c_str(), 0755);
            if (_storage_path.back() != '/')
                _storage_path.push_back('/');
        }

        ~FileServeiceImpl()
        {
        }

        // 请求单个文件
        void GetSingleFile(google::protobuf::RpcController *controller,
                           const ::chen_im::GetSingleFileReq *request,
                           ::chen_im::GetSingleFileRsp *response,
                           ::google::protobuf::Closure *done)
        {
            brpc::ClosureGuard rpc_guard(done);
            response->set_request_id(request->request_id());
            // 1, 取出请求中的文件ID(本质也是文件名)
            std::string file_id = request->file_id();
            std::string filename = _storage_path + file_id;
            // 2, 根据文件id读取文件数据
            std::string body;
            bool ret = readFile(filename, body);
            if (ret == false)
            {
                response->set_errmsg("read failed");
                response->set_success(false);
                LOG_ERROR("{}读取文件失败", request->request_id());
                return;
            }

            // 组织响应
            response->set_success(true);
            response->mutable_file_data()->set_file_id(file_id);
            response->mutable_file_data()->set_file_content(body);
        }

        // 请求多个文件
        void GetMultiFile(google::protobuf::RpcController *controller,
                          const ::chen_im::GetMultiFileReq *request,
                          ::chen_im::GetMultiFileRsp *response,
                          ::google::protobuf::Closure *done)
        {
            brpc::ClosureGuard rpc_guard(done);
            response->set_request_id(request->request_id());
            // 循环取出请求中的文件ID，读取文件数据进行填充
            for (int i = 0; i < request->file_id_list_size(); i++)
            {
                std::string fid = request->file_id_list(i);
                std::string filename = _storage_path + fid;
                std::string body;
                bool ret = readFile(filename, body);
                if (ret == false)
                {
                    response->set_success(false);
                    response->set_errmsg("读取文件数据失败！");
                    LOG_ERROR("{} 读取文件数据失败！", request->request_id());
                    return;
                }
                FileDownloadData data;
                data.set_file_id(fid);
                data.set_file_content(body);
                response->mutable_file_data()->insert({fid, data});
            }
            response->set_success(true);
        }

        //上传单个文件
        void PutSingleFile(google::protobuf::RpcController *controller,
                           const ::chen_im::PutSingleFileReq *request,
                           ::chen_im::PutSingleFileRsp *response,
                           ::google::protobuf::Closure *done)
        {
            brpc::ClosureGuard rpc_guard(done);
            response->set_request_id(request->request_id());
            // 1. 为文件生成一个唯一uudi作为文件名 以及 文件ID
            std::string fid = uuid();
            std::string filename = _storage_path + fid;
            // 2. 取出请求中的文件数据，进行文件数据写入
            bool ret = writeFile(filename, request->file_data().file_content());
            if (ret == false)
            {
                response->set_success(false);
                response->set_errmsg("读取文件数据失败！");
                LOG_ERROR("{} 写入文件数据失败！", request->request_id());
                return;
            }
            // 3. 组织响应
            response->set_success(true);
            response->mutable_file_info()->set_file_id(fid);
            response->mutable_file_info()->set_file_size(request->file_data().file_size());
            response->mutable_file_info()->set_file_name(request->file_data().file_name());
        }

        //上传多个文件
        void PutMultiFile(google::protobuf::RpcController *controller,
                          const ::chen_im::PutMultiFileReq *request,
                          ::chen_im::PutMultiFileRsp *response,
                          ::google::protobuf::Closure *done)
        {
            brpc::ClosureGuard rpc_guard(done);
            response->set_request_id(request->request_id());
            for (int i = 0; i < request->file_data_size(); i++)
            {
                std::string fid = uuid();
                std::string filename = _storage_path + fid;
                bool ret = writeFile(filename, request->file_data(i).file_content());
                if (ret == false)
                {
                    response->set_success(false);
                    response->set_errmsg("读取文件数据失败！");
                    LOG_ERROR("{} 写入文件数据失败！", request->request_id());
                    return;
                }
                chen_im::FileMessageInfo *info = response->add_file_info();
                info->set_file_id(fid);
                info->set_file_size(request->file_data(i).file_size());
                info->set_file_name(request->file_data(i).file_name());
            }
            response->set_success(true);
        }

    private:
        std::string _storage_path;
    };


    class FileServer
    {
    public:
        using ptr = std::shared_ptr<FileServer>;
        FileServer(const Registry::ptr& reg_client, const std::shared_ptr<brpc::Server>& server)
            :_reg_client(reg_client)
            ,_rpc_server(server)    
        {}

        void start()
        {
            _rpc_server->RunUntilAskedToQuit();
        }

    private:
        Registry::ptr _reg_client;
        std::shared_ptr<brpc::Server> _rpc_server;
    };


    class FileServerBuilder
    {
    public:
        //服务注册中心地址， 服务器监控的跟目录+实例名称   当前实例的外部访问地址(ip + port)
        void make_reg_object(const std::string& reg_host, const std::string& service_name, const std::string& access_host)
        {
            _reg_client = std::make_shared<Registry>(reg_host);
            _reg_client->registry(service_name , access_host);
        }

        //构造rpc服务器对象
        void make_rpc_server(uint16_t port, int32_t timeout, uint8_t num_threads, const std::string& path = "./data")
        {
            _rpc_server = std::make_shared<brpc::Server>();
            FileServeiceImpl* file_service = new FileServeiceImpl(path);
            int ret = _rpc_server->AddService(file_service, brpc::ServiceOwnership::SERVER_OWNS_SERVICE);
            if(ret == -1)
            {
                LOG_ERROR("添加rcp服务失败");
                abort();
            }

            brpc::ServerOptions options;
            options.idle_timeout_sec = timeout;
            options.num_threads = num_threads;
            ret = _rpc_server->Start(port, &options);
            if(ret == -1)
            {
                LOG_ERROR("服务启动失败");
                abort();
            }
        }

        FileServer::ptr build()
        {
            if(!_reg_client)
            {
                LOG_ERROR("未初始化服务注册模块");
                abort();
            }

            if(!_rpc_server)
            {
                LOG_ERROR("未初始化rpc服务器模块");
                abort();
            }

            FileServer::ptr server = std::make_shared<FileServer>(_reg_client, _rpc_server);
            return server;
        }

    private:
        Registry::ptr _reg_client;
        std::shared_ptr<brpc::Server> _rpc_server;
    };
}